package com.aggron.concurrency.concurrency;

import java.util.Arrays;
import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author LR2SD
 * @description
 * @date 2018-09-19 20:49
 */
public class CopyOnWriteArrayListExample2 {
    public static void main(String [] args){
        CopyOnWriteArrayList<Integer> copyOnWriteArrayList=new CopyOnWriteArrayList<>();

        copyOnWriteArrayList.add(1);
        copyOnWriteArrayList.add(2);
        copyOnWriteArrayList.add(3);
        //test1(copyOnWriteArrayList);
        test2(copyOnWriteArrayList);
    }

    public static void test1(CopyOnWriteArrayList<Integer> copyOnWriteArrayList){
        for(int i=0;i<copyOnWriteArrayList.size();i++){
            if(copyOnWriteArrayList.get(i).equals(3)){
                copyOnWriteArrayList.remove(i);
            }
        }
    }

    public static void test2(CopyOnWriteArrayList<Integer> copyOnWriteArrayList){
        Iterator<Integer> iterator=copyOnWriteArrayList.iterator();
        while(iterator.hasNext()){
            Integer  integer=iterator.next();
            if(integer.equals(3)){
                copyOnWriteArrayList.remove(integer);
            }
        }
        System.out.println(copyOnWriteArrayList);
    }

    public static void test3(CopyOnWriteArrayList<Integer> copyOnWriteArrayList){
        for (Integer integer:copyOnWriteArrayList) {
            if(integer.equals(3)){
                copyOnWriteArrayList.remove(integer);
            }
        }
    }
}
